<template>
  <!-- 编辑管理员弹框 -->
  <div class="dialog-content">
    <el-dialog v-model="state.isShowCreatAdminDialog" width="400px" :title="`编辑${dialogTitle}`">
      <el-form class="login-form" :label-position="labelPosition" :rules="rules" :model="formData">
        <el-form-item label="账号" prop="account">
          <el-input v-model="formData.account" disabled />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="formData.sex" placeholder="请选择性别">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-select v-model="formData.department" placeholder="请选择部门">
            <el-option v-for="(item, index) in props.departmentList" :key="index" :label="item"
              :value="item" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="state.isShowCreatAdminDialog = false">取消</el-button>
          <el-button type="primary" @click="editAdmin">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { updateAdminInfoAPI, searchUserByAccountAPI } from '../../../api/userinfo'
import { defineProps } from 'vue'
import { tracking } from '../../../utils/operate.js'


interface Department {
  department: string
}

const labelPosition = ref('top')
const props = defineProps<{
  getFirstPage: Function,
  departmentList: Department[]
}>()



const dialogTitle = ref()

const state = reactive({
  isShowCreatAdminDialog: false,
})


interface formData {
  account: string,
  name: string,
  sex: string,
  email: string,
  department: string
  identity: string,
  id: string
}

const formData: formData = reactive({
  account: '',
  name: '',
  sex: '',
  email: '',
  department: '',
  identity: '',
  id: ''
})


// 表单校验
const rules = reactive({
  name: [
    { required: true, message: '请输入管理员的名字', trigger: 'blur' },
  ],
  sex: [
    { required: true, message: '请输入管理员的性别', trigger: 'blur' },
  ],
  email: [
    { required: true, message: '请输入管理员的邮箱', trigger: 'blur' },
  ],
  department: [
    { required: true, message: '请输入管理员的部门', trigger: 'blur' },
  ],
})

// 打开弹框
const openDialog = async (account, id, title) => {

  state.isShowCreatAdminDialog = true
  dialogTitle.value = title
  formData.id = id

  const res = await searchUserByAccountAPI({ account, identity: title})

  formData.account = res.data.data[0].account
  formData.name = res.data.data[0].name
  formData.sex = res.data.data[0].sex
  formData.email = res.data.data[0].email
  formData.department = res.data.data[0].department
  formData.identity = res.data.data[0].identity
}

// 编辑管理员
const editAdmin = async () => {

  const res = await updateAdminInfoAPI(formData)

  if (res.data.status === 0) {
    ElMessage({
      message: res.data.message,
      type: 'success'
    })
    state.isShowCreatAdminDialog = false
    if (props.getFirstPage) {
      props.getFirstPage()
    }
    tracking("用户管理", "编辑", formData.id , "低级")
  } else {
    ElMessage({
      message: res.data.message,
      type: 'error'
    })
  }

}




defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
.dialog-content {
  display: flex;
  padding: 20px 30px;
}

:deep(.el-form-item) {
  margin: 30px;
}
</style>